package org.example.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.PoemDao;
import org.example.entity.Poem;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;
import org.example.service.PoemService;
import utils.ES;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PoemServiceImpl implements PoemService {

    @Resource
    PoemDao poemDao;

    @Override
    public Boolean addPoem(Poem poem) {
        return poemDao.insert(poem)>0;
    }

    @Override
    public Boolean deletePoem(Long poemId) {
        return poemDao.deleteById(poemId)>0;
    }

    @Override
    public Boolean updatePoem(Poem poem) {
        return poemDao.updateById(poem)>0;
    }

    @Override
    public Poem getPoem(Long poemId) {
        return poemDao.selectById(poemId);
    }

    @Override
    public List<Poem> getPoemByType(String type, Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("type",type);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getPoemByWriter(String writer,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("author",writer);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getPoemByDynasty(String dynasty,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("dynasty",dynasty);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getAllPoem(Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getPoemByTitle(String title,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("title",title);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getPoemByPassage(String passage,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("passage",passage);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

//    @Override
//    public List<Poem> fuzzySearch(String field,Integer cnt) {
//        Page<Poem> page=new Page<Poem>(cnt,10);
//        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
//        wrapper.like("title",field).or()
//                .like("author",field).or()
//                .like("dynasty",field).or()
//                .like("type",field).or()
//                .like("passage",field);
//        return poemDao.selectPage(page,wrapper).getRecords();
//    }

    @Override
    public Poem getOneByTitle(String title) {
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("title",title);
        if(poemDao.selectByMap(map)!=null){
            return poemDao.selectByMap(map).get(0);
        }
        return null;
    }

    @Override
    public  List<Poem>  fuzzySearch(String field,Integer cnt) throws Exception {
        RestHighLevelClient client= ES.getClient();
        List<Poem> result= new ArrayList<Poem>() ;
        SearchRequest mySearchRequest = new SearchRequest("poem");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery= QueryBuilders.boolQuery();
        boolQuery.should(QueryBuilders.matchQuery("title",field))
                .should(QueryBuilders.termQuery("dynasty",field))
                .should(QueryBuilders.termQuery("author",field))
                .should(QueryBuilders.matchQuery("type",field))
                .should(QueryBuilders.matchQuery("passage",field));
        builder.query(boolQuery);
        mySearchRequest.source(builder);
        SearchResponse search = client.search(mySearchRequest, RequestOptions.DEFAULT);
        for (SearchHit hit : search.getHits().getHits()) {
            Map<String, Object> tempresult=hit.getSourceAsMap();
            result.add(JSONObject.toJavaObject(JSONObject.parseObject(JSONObject.toJSONString(tempresult)),Poem.class));
        }
        return result;
    }

}

